//by wj59789 2016.2.20 本地敌人沙包可以挡子弹
//k1升级 2021.9.23

if (isNil "Sacks_isDown") then {
	Sacks_isDown = {
		animationState _this in ["ainjppnemstpsnonwrfldnon_injuredhealed","ainjppnemstpsnonwrfldnon","unconscious"]
	};
	fn_Sacks_close = {
		Sacks_act_hide = true;
		inGameUISetEventHandler ["Action", "false"];
		(findDisplay 46) displayRemoveEventHandler ["KeyDown", _X_Key];
		(findDisplay 46) displayRemoveEventHandler ["MouseMoving", _MMH];
	};
};

Sacks_act_hide = false;
Sacks_act_ensure = true;
Sacks_act_cancel = true;
Sacks_blindage_ensure = false;
Sacks_blindage_cancel = false;

private ["_MMH","_X_Key"];
_unit = player;
_dir = getDir _unit;
if(isNil "Sacks_blindage_class")then {Sacks_blindage_class = "Land_BagFence_End_F"};
_obj = Sacks_blindage_class createVehicleLocal [0,0,0];
_bbr = boundingBoxReal _obj;
_p1 = _bbr select 0;
_p2 = _bbr select 1;
_maxLength = abs ((_p2 select 1) - (_p1 select 1));
if(_maxLength < 3)then {_maxLength = 1};
_relPos = [_unit, _maxLength, _dir] call BIS_fnc_relPos;
_h = getPosASL _unit select 2;
_relPos set [2, _h];
_obj setPosASL _relPos;
_obj attachTo [_unit];
Sacks_blindage_obj = _obj;

["Sacks_blindage_FrameLoop", "onEachFrame", {
	{
		if (alive _x && {Sacks_blindage_obj distance _x < 4 || {{alive _x} count crew _x > 0}}) then {drawIcon3D ["\A3\ui_f\data\map\markers\handdrawn\objective_CA.paa", [1,0,0,1], _x, 1, 1, 0, "附近物体", 2, .04, "PuristaMedium", "center", true]};
	} forEach (nearestObjects [Sacks_blindage_obj, ["LandVehicle", "Air", "Ship"], 10]);
}] call BIS_fnc_addStackedEventHandler;

_idx = player addEventHandler ["Fired", {Sacks_blindage_cancel = true}];

waitUntil {Sacks_blindage_ensure || {Sacks_blindage_cancel} || {stance player != 'STAND'} || {_unit call Sacks_isDown} || {!alive _unit} || {vehicle _unit != _unit}};
["Sacks_blindage_FrameLoop", "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
player removeEventHandler ["fired", _idx];
detach _obj;
Sacks_act_ensure = false;
Sacks_act_cancel = false;

if (Sacks_blindage_cancel || {stance player != 'STAND'} || {_unit call Sacks_isDown} || {!alive _unit} || {vehicle _unit != _unit}) exitWith {deleteVehicle _obj; Sacks_act_hide = true; TitleText ["取消", "Plain Down", .5]};

_vehs = [];
{
	if (alive _x && {_obj distance _x < 4 || {{alive _x} count crew _x > 0}}) then {_vehs pushback _x};
} forEach (nearestObjects [_obj, ["LandVehicle", "Air", "Ship"], 10]);

if (count _vehs > 0) exitWith {deleteVehicle _obj; Sacks_act_hide = true; TitleText ["无法在此处建造", "Plain Down", .5]};

_pos = getPosASL _obj;
_dir = getDir _obj;
deleteVehicle _obj;
TitleText ["建造中...", "Plain Down"];
Sacks_dir_save = getDir player;
_MMH = (findDisplay 46) displayAddEventHandler ["MouseMoving", {player setDir Sacks_dir_save}];
sleep .5;
Sacks_X_cancel = false;
_time1 = time + 1.5;
if(currentWeapon _unit != primaryWeapon _unit || {currentWeapon _unit == ""})then{_unit switchMove "AmovPknlMstpSrasWrflDnon"};
_unit playMove "AinvPknlMstpSnonWrflDr_medic5";
inGameUISetEventHandler ["Action", "true"];
waitUntil {animationState _unit in ["ainvpknlmstpsnonwrfldr_medic5","ainjppnemstpsnonwrfldnon_injuredhealed","ainjppnemstpsnonwrfldnon"] || {time > _time1}};

TitleText ["按X键取消建造", "Plain Down"];
_X_Key = (findDisplay 46) displayAddEventHandler ["KeyDown", {if((_this select 1) == 45)then {Sacks_X_cancel = true}}];

_time2 = time + 10;
_time3 = time + 7;
waitUntil {animationState _unit != "ainvpknlmstpsnonwrfldr_medic5" || {!alive _unit} || {Sacks_X_cancel} || {time > _time2}};

call fn_Sacks_close;

_isDown = _unit call Sacks_isDown;
if (_isDown || {!alive _unit} || {Sacks_X_cancel} || {vehicle _unit != _unit} || {time < _time3}) exitWith {
    if(!_isDown && {alive _unit} && {vehicle _unit == _unit})then{_unit switchMove "amovpknlmstpsraswrfldnon"};
	TitleText ["建造被中断", "Plain Down", .5];
};

_obj = Sacks_blindage_class createVehicle [0,0,0];
_obj setDir _dir;
_obj setPosASL _pos;
Sacks_blindage_arr pushback _obj;
publicVariable "Sacks_blindage_arr";
TitleText ["完成", "Plain Down", .5];


